package com.peas.platform.module.icims.budget.archives.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.budget.archives.api.req.SaveBudgetItemTypeRange;
import com.peas.platform.module.icims.budget.archives.controller.vo.BudgetItemTypeRangeVo;
import com.peas.platform.module.icims.budget.archives.domain.BudgetItemTypeDomain;
import com.peas.platform.module.icims.budget.archives.dto.BudgetItemTypeDto;
import com.peas.platform.module.icims.budget.archives.dto.BudgetItemTypeRangeDto;
import com.peas.platform.module.icims.budget.archives.service.BudgetItemTypeRangeService;
import com.peas.platform.module.icims.budget.archives.service.BudgetItemTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BudgetItemTypeServiceImpl extends BaseServiceImpl<BudgetItemTypeDto> implements BudgetItemTypeService {

    @Resource
    private BudgetItemTypeDomain budgetItemTypeDomain;
    @Resource
    private BudgetItemTypeRangeService budgetItemTypeRangeService;

    @Override
    public BaseDomain<BudgetItemTypeDto, ? extends BasePo> getDomain() {
        return budgetItemTypeDomain;
    }

    @Transactional
    @Override
    public void saveBudgetItemTypeRange(SaveBudgetItemTypeRange vo) {
        List<BudgetItemTypeRangeDto> list = budgetItemTypeRangeService.listByField("itemTypeId", vo.getItemTypeId());
        budgetItemTypeRangeService.removeBatchByList(list);
        List<BudgetItemTypeRangeDto> itrds = new ArrayList<>();
        CollUtil.forEach(vo.getItemRanges(), (CollUtil.Consumer<BudgetItemTypeRangeVo>) (v, index) -> {
            BudgetItemTypeRangeDto dto = BeanUtil.copyProperties(v, BudgetItemTypeRangeDto.class);
            dto.setItemTypeId(vo.getItemTypeId());
            itrds.add(dto);
        });
        budgetItemTypeRangeService.saveBatch(itrds);
    }

}
